#include<stdio.h>
#include<stdlib.h>
#include "q1.h"

status init(queue *q){
	q->front=0;
	q->rear=0;
	q->count=0;
}

status is_empty(queue *q){
	return q->count==0;
}

status append(queue *q,datatype e){
	if(q->count>0 && q->rear==q->front){
		return error;
	}else{
		q->data[q->rear]=e;
		q->rear=(q->rear+1)%maxsize;
		q->count++;
		return ok;
	}
}

status del(queue *q,datatype *e){
	if(q->count==0){
		return error;
	}else{
		*e=q->data[q->front];
		q->front=(q->front+1)%maxsize;
		q->count--;
		return ok;
	}
}

status get_front(queue *q,datatype *e){
	if(q->count==0){
		return error;
	}else{
		*e=q->data[q->front];
		return ok;
	}
}
void print_rd(datatype d){
	printf("%d ",d);
}
void traverse(queue *q,void(*print_d)(datatype)){
	if(is_empty(q))return;
	if(q->front<q->rear){
		for(int i=q->front;i<q->rear;i++){
			print_d(q->data[i]);
		}
	}else{
		for(int i=q->front;i<maxsize;i++){
			print_d(q->data[i]);
		}
		for(int i=0;i<q->rear;i++){
			print_d(q->data[i]);
		}
	}
	printf("\n");
}
int main(){
	queue *q;
	q=(queue*)malloc(sizeof(queue));
	if(!q)exit(overflow);
	append(q,1);
	append(q,2);
	append(q,3);
	append(q,4);
	append(q,5);
	append(q,6);
	traverse(q,print_rd);

}

